#include <iostream>
#include <unistd.h>
#include <pthread.h>

int* p = nullptr;

void* threadrun(void* args)
{
    int a = 123;

    p = &a;

    while(true)
    {
        sleep(1);
    }

    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadrun, nullptr);

    while(true)
    {
        // 主线程也能访问子线程栈空间
        std::cout << "*p: " << *p << std::endl;
        sleep(1);
    }

    pthread_join(tid, nullptr);
    return 0;
}